﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MediaWPF
{
    static class getFileTags
    {
        public struct tagStruct
        {
           public String _grouping;
           public String _title;
           public String _artists;
           public String _performers;
           public String _composers;
           public String _conductor;
           public String _album;
           public String _comment;
           public String _copyright;
           public String _genres;
           public String _bpm;
           public String _year;
           public String _track;
           public String _trackCount;
           public String _disc;
           public String _discCount;
           public String _lyrics;
           public String _mediatypel;
        };

        public static List<tagStruct> getFileTagsFromPaths(string[] args)
        {
            List<tagStruct> tagstruct = new List<tagStruct>();

            if (args.Length == 0)
            {
                Console.Error.WriteLine("USAGE: mono ReadFromUri.exe PATH [...]");
                return null;
            }

           // Gnome.Vfs.Vfs.Initialize();

            DateTime start = DateTime.Now;
            int songs_read = 0;
            try
            {
                foreach (string path in args)
                {
                    string uri = path;
                    Console.WriteLine(uri);
                    TagLib.File file = null;

                    try
                    {
                        System.IO.FileInfo file_info = new System.IO.FileInfo(uri);
                //        uri = Gnome.Vfs.Uri.GetUriFromLocalPath(file_info.FullName);
                    }
                    catch
                    {
                    }

                    try
                    {
                        file = TagLib.File.Create(new VfsFileAbstraction(uri));
                    }
                    catch (TagLib.UnsupportedFormatException)
                    {
                        Console.WriteLine("UNSUPPORTED FILE: " + uri);
                        Console.WriteLine(String.Empty);
                        Console.WriteLine("---------------------------------------");
                        Console.WriteLine(String.Empty);
                        continue;
                    }

                    tagStruct newtabstruct = new tagStruct();



                    Console.WriteLine("Tags on disk:   " + file.TagTypesOnDisk);
                    Console.WriteLine("Tags in object: " + file.TagTypes);
                    Console.WriteLine(String.Empty);
                    newtabstruct._grouping = file.Tag.Grouping;
                    newtabstruct._title =  file.Tag.Title;
                    newtabstruct._artists = file.Tag.AlbumArtists == null ? String.Empty : System.String.Join("", file.Tag.AlbumArtists);
                    newtabstruct._performers = file.Tag.Performers == null ? String.Empty : System.String.Join("", file.Tag.Performers);
                    newtabstruct._composers = (file.Tag.Composers == null ? String.Empty : System.String.Join("", file.Tag.Composers));
                    newtabstruct._conductor = file.Tag.Conductor;
                    newtabstruct._album = file.Tag.Album;
                    newtabstruct._comment = file.Tag.Comment;
                    newtabstruct._copyright = file.Tag.Copyright;
                    newtabstruct._genres = (file.Tag.Genres == null ? String.Empty : System.String.Join("", file.Tag.Genres));
                  // newtabstruct._bpm = file.Tag.BeatsPerMinute;
                  // newtabstruct._year = file.Tag.Year ;
                  // newtabstruct._track = file.Tag.Track;
                  // newtabstruct._trackCount = file.Tag.TrackCount;
                  // newtabstruct._disc = file.Tag.Disc;
                  // newtabstruct._discCount = file.Tag.DiscCount;
                    newtabstruct._lyrics = file.Tag.Lyrics;
                  // newtabstruct._mediatypel = file.Properties.MediaTypes;

                     tagstruct.Add(newtabstruct);
                    
                    Console.WriteLine("Media Types: " + file.Properties.MediaTypes + "\n");

                    foreach (TagLib.ICodec codec in file.Properties.Codecs)
                    {
                        TagLib.IAudioCodec acodec = codec as TagLib.IAudioCodec;
                        TagLib.IVideoCodec vcodec = codec as TagLib.IVideoCodec;

                        if (acodec != null && (acodec.MediaTypes & TagLib.MediaTypes.Audio) != TagLib.MediaTypes.None)
                        {
                            Console.WriteLine("Audio Properties : " + acodec.Description);
                            Console.WriteLine("Bitrate:    " + acodec.AudioBitrate);
                            Console.WriteLine("SampleRate: " + acodec.AudioSampleRate);
                            Console.WriteLine("Channels:   " + acodec.AudioChannels + "\n");
                        }

                        if (vcodec != null && (vcodec.MediaTypes & TagLib.MediaTypes.Video) != TagLib.MediaTypes.None)
                        {
                            Console.WriteLine("Video Properties : " + vcodec.Description);
                            Console.WriteLine("Width:      " + vcodec.VideoWidth);
                            Console.WriteLine("Height:     " + vcodec.VideoHeight + "\n");
                        }
                    }

                    if (file.Properties.MediaTypes != TagLib.MediaTypes.None)
                        Console.WriteLine("Length:     " + file.Properties.Duration + "\n");

                 /*   IPicture[] pictures = file.Tag.Pictures;

                    Console.WriteLine("Embedded Pictures: " + pictures.Length);

                    foreach (IPicture picture in pictures)
                    {
                        Console.WriteLine(picture.Description);
                        Console.WriteLine("   MimeType: " + picture.MimeType);
                        Console.WriteLine("   Size:     " + picture.Data.Count);
                        Console.WriteLine("   Type:     " + picture.Type);
                    }
                    */
                    Console.WriteLine(String.Empty);
                    Console.WriteLine("---------------------------------------");
                    Console.WriteLine(String.Empty);

                    songs_read++;
                }
            }
            finally
            {
          //      Gnome.Vfs.Vfs.Shutdown();
            }

            DateTime end = DateTime.Now;

            Console.WriteLine("Total running time:    " + (end - start));
            Console.WriteLine("Total files read:      " + songs_read);
            Console.WriteLine("Average time per file: " + new TimeSpan((end - start).Ticks / songs_read));
            return tagstruct;
        }
    }

    public class VfsFileAbstraction : TagLib.File.IFileAbstraction
    {
        private string name;

        public VfsFileAbstraction(string file)
        {
            name = file;
        }

        public string Name
        {
            get { return name; }
        }

        public System.IO.Stream ReadStream
        {
            get { return new FileStream(Name, System.IO.FileMode.Open); }
        }

        public System.IO.Stream WriteStream
        {
            get { return new FileStream(Name, System.IO.FileMode.Open); }
        }

        public void CloseStream(System.IO.Stream stream)
        {
            stream.Close();
        }
    }

}
